package com.tzy.note.base.thread;

import org.junit.jupiter.api.Test;

/**
 * @author tangzanyong
 * @description 多线程实现方式二：实现Runnable接口
 * @date 2020/4/16
 **/
public class Thread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("当前线程：" + Thread.currentThread().getName());
    }

    @Test
    public void test(){
        Thread thread = new Thread(new Thread2());
        thread.setName("线程A");
        thread.start();//开启线程
    }

    //使用匿名内部类继承父类
    @Test
    public void testAnonyInnerExtends(){
        new Thread("线程A"){ //1.继承Thread类
            @Override
            public void run() { //2.重写run方法
                //3.将要执行的代码，写在run方法中
                System.out.println("当前线程：" + Thread.currentThread().getName());
            }
        }.start();//开启线程
    }
    //使用匿名内部类实现接口
    @Test
    public void testAnonyInnerImplements(){
        new Thread(new Runnable() { //1.将Runnable的子类对象传递给Thread的构造方法
            @Override
            public void run() { //2.重写run方法
                //3.将要执行的代码，写在run方法中
                System.out.println("当前线程：" + Thread.currentThread().getName());
            }
        },"线程A").start();//开启线程
    }
    //使用java8实现
    @Test
    public void testJava8(){
        new Thread(()->{
            System.out.println("当前线程：" + Thread.currentThread().getName());
        },"线程A").start();//开启线程
    }
}
